<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJAEmployers extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct( $location = array() )
    {    	
        parent::__construct( $location );  
        checkPermission_Administrator('employer');    
        // Register Extra tasks
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'unblock', 	'block');
		$this->registerTask( 'unapprove', 	'approve');
		$this->registerTask( 'orderup',    'orderdown' );
		
    }
    
    /**
    * Display current jaemployers of the component to administrator
    * 
    */
    function display(){
    	global $jbconfig;
    	switch(JRequest::getVar('task'))
		{			
			case 'user':
			{
				JRequest::setVar( 'layout', 'form' );				
			} break;
			case 'profile'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'profile' );
				
			} break;
			case 'account':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'account' );
			} break;
			case 'invoices':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'invoices' );
			} break;
		}
		
		parent::display();
    }
    
    /**
     * Edit user, redirect to user management
     *
     */
    function editUser(){

    	$cid 	= JRequest::getVar( 'cid', array(), '', 'array' );

		JArrayHelper::toInteger( $cid );
		
    	if(is_array($cid)) $cid = $cid[0];
    	if(!$cid) return ;
    	
    	$this->setRedirect("index.php?option=com_users&view=user&task=edit&cid[]=$cid");        
    }
    
    /**
	 * Edit content of email
	 *
	 * @return void
	 */
	function sendmail(){			    
	    global $option;
	    $cid = JRequest::getVar('cid',array(), 'post', 'array');
	    JArrayHelper::toInteger( $cid );
	    $cid = implode(',',$cid);	    
	    $this->setRedirect("index.php?option=$option&view=jamail&cid=$cid&pre_view=jaemployers");
	}
	
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jaemployers");
    }   
    /**
    * Remove a jaemployers row
    * 
    */   
    function remove(){
    	global $option;
 
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        
		$cid 			= JRequest::getVar( 'cid', array(), '', 'array' );
		$msg = '';
		$allow_del = true;
//		$job_exist = false;
//		foreach ($cid as $id)
//		{
//			$joblist = getItemByFieldName('ja_jobs','user_id',$id);
//			if (count($joblist)>=1)
//			{
//				$allow_del = false;
//				$user = JFactory::getUser($id);
//				$job_exist = true;
//				
//			}
//		}
			
		$trans_exist = false;	
		foreach ($cid as $id){
			$translist = getItemByFieldName('ja_transactions','user_id',$id);
			if (count($translist)>=1){
				$allow_del = true;
				$user = JFactory::getUser($id);
				$trans_exist = true;
				
			}
		}

		if ($trans_exist  && !$allow_del)
			JError::raiseWarning(1,  JText::_("You can not delete")." '$user->username'. ".JText::_("Please delete all transaction of")." '$user->username' ".JText::_("before."));
		
		if (!$allow_del){
			$this->setRedirect("index.php?option=$option&view=jaemployers", $msg);
			return ;
		}
		
        $model =& $this->getModel('jaemployers');
        if(($msg = $model->remove()) == ''){
//             $this->setMessage(JText::_('Successfully deleted profile(s)'));
             $msg = JText::_('Successfully deleted profile(s)');
		}
		$this->setRedirect("index.php?option=$option&view=jaemployers", $msg);
	}
		
	function saveorder()
    {
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);

        $model =& $this->getModel( 'jaemployers' );
        $msg = '';
        if ($model->setOrder($cid)) {
            $msg = JText::_( 'New ordering saved' );
        } else {
        	JError::raiseWarning(1, $model->getError());            
        }
        $this->setRedirect(  "index.php?option=$option&view=jaemployers", $msg );
    }
    
    
    function orderdown(){
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);
        
        $task = JRequest::getVar('task', 'orderdown');

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
			JError::raiseWarning(1,  JText::_('No Items Selected'));
            $this->setRedirect( "index.php?option=$option&view=jaemployers" );
        }

        $model =& $this->getModel( 'jaemployers' );
        $movement = $task == 'orderdown' ? 1 : -1;

        if($model->orderItem($id, $movement)){
            $msg = $movement > 0 ? JText::_( 'Item Moved Down' ) : JText::_( 'Item Moved Up' );
        }
        
        $this->setRedirect( "index.php?option=$option&view=jaemployers", $msg );
    }    
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
				
		$model	=& $this->getModel('jaemployers');
		$post	= JRequest::get('post');		
				
//		if(isset($_FILES)){		
//			$dest = JPATH_SITE.DS.'components'.DS.$option.DS.'uploads'.DS.'profiles';
//			
//			foreach ($_FILES as $k=>$F){				
//				if(isset($F['name']) && $F['name']!=''){
//					JFile::upload($F['tmp_name'], $dest.DS.date('Ymdhms').$F['name']);
//					$post[$k] = 'components/'.$option.'/uploads/profiles/'.date('Ymdhms').$F['name'];
//				}
//				else 
//					$post[$k] = JRequest::getVar('text_'.$k);
//			}
//		}
		
		foreach ($post as $k=>$value){
			if($k!='cid' && is_array($value)){
				$post[$k] = implode(',', $value);				
			}
			else{
				$post [$k] = trim($value);
			}
		}
				
		$model->setState( 'request', $post );
		$msg = '';
		if ($id = $model->store()) {
			$msg = JText::_( 'Successfully updated Profile' );
		} else {
			JRequest::setVar('task','profile');
			parent::display();		
			return;
		}

		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jaemployers&task=profile&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jaemployers", $msg );
				break;
		}
	}
		
	function employer_save()
	{
		global $option;
		$cid 	= JRequest::getVar( 'cid', array(), '', 'array' );
		JArrayHelper::toInteger( $cid );
    	if(is_array($cid)) $cid = $cid[0];
		$model	=& $this->getModel('jaemployers');
		
		if ($model->empl_store())
		{
			$msg = JText::_('Successfully updated Account information!');
			$this->setRedirect("index.php?option=$option&view=jaemployers&task=account&cid[]=$cid",$msg);
		}
		else 
		{
			JError::raiseWarning(500,JText::_('Account information was not saved. Please check information !'));
			$this->setRedirect("index.php?option=$option&view=jaemployers&task=account&cid[]=$cid");
		}
	}
	
	/**
	* change Is_Published status
	*/
	/**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function block(){
		global  $option;
		
		if($this->getTask()=='unblock'){
			$publish = 0;
		}
		else{
			$publish = 1;
		}
		
		$model = $this->getModel('jaemployers');
		
		$msg = $model->doblock($publish);
		
		$cache = & JFactory::getCache('com_users');
		$cache->clean();	
		$this->setRedirect("index.php?option=$option&view=jaemployers", $msg);
	}	

	 function download(){
         $model    =& $this->getModel( $this->get('jaemployers') );    
         $model->download();
         exit;  
    }
    function remove_attach()
    {
    	$model    =& $this->getModel( 'jaemployers' );
    	$model->remove_attach();
         exit;
    }
    function approve()
    {
    	global $option;
    	$model = $this->getModel('jaemployers');
    	$msg = $model->doapprove($this->getTask());
    	$this->setRedirect("index.php?option=$option&view=jaemployers",$msg);
    }
	       
}